<?php
/**
 * Created by Pako
 * Date: 2018/8/19
 * Time: 17:08
 */

namespace App\Http\Request\Auth;


use App\Http\Request\Store;
use App\Models\Auth\Role;

class RoleStore extends Store
{

    /**
     *
     * rules
     * @author Paco
     * @createTime 2018/8/21 18:30
     * @return array
     */
    public function rules()
    {

        $rules = [
            'name' => 'required|unique:auth_role,name',
            'key_name' => 'required|unique:auth_role,key_name',
            'permissions' => 'required|array',
            'permissions.*' => 'filled|required',
        ];
        $method = strtoupper($this->method());
        if ($method == 'PUT') {
            return [
                'role_id' => 'required',
                'key_name' => 'filled',
                'permissions' => 'filled',
                'permissions.*' => 'filled',
            ];
        }
        return $rules;
    }

    /**
     *
     * attributes
     * @author Paco
     * @createTime 2018/8/21 18:30
     * @return array
     */
    public function attributes()
    {
        return [
            'name' => '角色名称',
            'permissions' => '权限',
            'role_id' => '角色ID'
        ];
    }

    /**
     *
     * after
     * @author Paco
     * @createTime 2018/8/21 18:30
     * @return bool|mixed
     */
    public function after()
    {
        $method = strtoupper($this->method());
        if ($method == 'PUT') {
            $params = $this->validateData();
            if (isset($params['key_name'])) {
                $role_id = Role::query()->where('key_name', $params['key_name'])->value('role_id');
                if ($role_id != $params['role_id']) {
                    return $this->throwRespondError(10401);
                }
            }

        }
        return true;
    }


}